package second.binarytree;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution13 {
    List<String> res = new ArrayList<>();
    LinkedList<Integer> vals = new LinkedList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return res;
        backtracing(root);
        return res;
    }

    public void backtracing(TreeNode node) {
        vals.add(node.val);
        if (node.left == null && node.right == null) {
            StringBuffer sb = new StringBuffer();
            for (int i : vals) {
                sb.append(i).append("->");
            }
            sb.delete(sb.length() - 2, sb.length());
            res.add(sb.toString());
            return;
        }

        if (node.left != null) {
            backtracing(node.left);
            vals.removeLast();
        }
        if (node.right != null) {
            backtracing(node.right);
            vals.removeLast();
        }
    }
}

